package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.opensymphony.xwork2.ActionContext;
import java.util.Date;
import com.lb.jwitter.util.ApplicationContextProvider;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblDKMHDotDKNhom;
import com.lb.jwitter.service.TblDKMHDotDKNhomService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;

import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblDKMHDotDKNhomInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public void setId(Long id) {
		this.id = id;
	}

	private String ma;

	public void setMa(String ma) {
		this.ma = ma;
	}

	public String getMa() {
		return ma;
	}

	private String ten;

	public void setTen(String ten) {
		this.ten = ten;
	}

	public String getTen() {
		return ten;
	}

	private int dotDKId;

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	public int getDotDKId() {
		return dotDKId;
	}

	private Collection<TblDKMHDotDK> tblDKMHDotDKs;

	public Collection<TblDKMHDotDK> getTblDKMHDotDKs() {
		return tblDKMHDotDKs;
	}

	private TblDKMHDotDKService tblDKMHDotDKService;

	private String ngayBatDau;

	public void setNgayBatDau(String ngayBatDau) {
		this.ngayBatDau = ngayBatDau;
	}

	public String getNgayBatDau() {
		return ngayBatDau;
	}

	private String ngayKetThuc;

	public void setNgayKetThuc(String ngayKetThuc) {
		this.ngayKetThuc = ngayKetThuc;
	}

	public String getNgayKetThuc() {
		return ngayKetThuc;
	}

	private String quyen;

	public void setQuyen(String quyen) {
		this.quyen = quyen;
	}

	public String getQuyen() {
		return quyen;
	}

	private int tuKhoaHoc;
	private int denKhoaHoc;
	private int tuSoTCNo;
	private int denSoTCNo;

	public int getTuKhoaHoc() {
		return tuKhoaHoc;
	}

	public void setTuKhoaHoc(int tuKhoaHoc) {
		this.tuKhoaHoc = tuKhoaHoc;
	}

	public int getDenKhoaHoc() {
		return denKhoaHoc;
	}

	public void setDenKhoaHoc(int denKhoaHoc) {
		this.denKhoaHoc = denKhoaHoc;
	}

	public int getTuSoTCNo() {
		return tuSoTCNo;
	}

	public void setTuSoTCNo(int tuSoTCNo) {
		this.tuSoTCNo = tuSoTCNo;
	}

	public int getDenSoTCNo() {
		return denSoTCNo;
	}

	public void setDenSoTCNo(int denSoTCNo) {
		this.denSoTCNo = denSoTCNo;
	}

	private TblDKMHDotDKNhom tblDKMHDotDKNhom;

	public TblDKMHDotDKNhom getTblDKMHDotDKNhom() {
		return tblDKMHDotDKNhom;
	}

	private Collection<TblDKMHDotDKNhom> tblDKMHDotDKNhoms;

	public Collection<TblDKMHDotDKNhom> getTblDKMHDotDKNhoms() {
		return tblDKMHDotDKNhoms;
	}

	private TblDKMHDotDKNhomService tblDKMHDotDKNhomService;

	private Collection<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets;

	public Collection<TblDKMHDotDKHocVienChiTiet> getTblDKMHDotDKHocVienChiTiets() {
		return tblDKMHDotDKHocVienChiTiets;
	}

	private TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService;

	private Collection<TblDKMHDotDKHocVien> tblDKMHDotDKHocViens;

	public Collection<TblDKMHDotDKHocVien> getTblDKMHDotDKHocViens() {
		return tblDKMHDotDKHocViens;
	}

	private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService;

	private int preInsert(Message msg, TblDKMHDotDKNhom obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblDKMHDotDKNhom obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "them")) {
				return "noprivilege";
			}
			tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService) context
					.getBean("tbl_DKMH_DotDK_NhomClient");
			tblDKMHDotDKService = (TblDKMHDotDKService) context
					.getBean("tbl_DKMH_DotDKClient");

			tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context
					.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient");

			tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService) context
					.getBean("tbl_DKMH_DotDK_HocVienClient");

			msg = new Message();
			tblDKMHDotDKNhom = new TblDKMHDotDKNhom();
			tblDKMHDotDKNhom.setMa(ma);
			tblDKMHDotDKNhom.setTen(ten);
			tblDKMHDotDKNhom.setDotDKId(tblDKMHDotDKService
					.gettblDKMHDotDK(new Long(dotDKId)));
			try {
				DateFormat df =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				Date dd = df.parse(ngayBatDau);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(dd);
				XMLGregorianCalendar d = DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(c);
				tblDKMHDotDKNhom.setNgayBatDau(d);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
				Date dd = df.parse(ngayKetThuc);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(dd);
				XMLGregorianCalendar d = DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(c);
				tblDKMHDotDKNhom.setNgayKetThuc(d);
			} catch (Exception e) {
				e.printStackTrace();
			}
			tblDKMHDotDKNhom.setQuyen(quyen);
			preInsert(msg, tblDKMHDotDKNhom);
			if (msg.isPreMessage_b()) {
				id = tblDKMHDotDKNhomService
						.createtblDKMHDotDKNhom(tblDKMHDotDKNhom);
				//tao chi tiet hoc vien dang ky cho nhom
		        tblDKMHDotDKNhomService.calSPCreateChiTietHVDangKyTheoNhomDK(new Long(dotDKId), id, tuKhoaHoc, denKhoaHoc, tuSoTCNo, denSoTCNo);
		           
				postInsert(msg, tblDKMHDotDKNhom);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			tblDKMHDotDKNhom.setId(id);
			tblDKMHDotDKs = tblDKMHDotDKService.findAlltblDKMHDotDK(0, 0);
			//tblDKMHDotDKHocViens = tblDKMHDotDKHocVienService
			//		.findAlltblDKMHDotDKHocVien(0, 0);
			if (pageSize == 0) 
			   { 
			       pageSize = 20; 
			   } 
			   if (page == 0) 
			   { 
			       page = 1; 
			   } 
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
			cons.clear(); 
			   if (id != 0) 
			   { 
			       ConditionReport con = new ConditionReport(); 
			       con.setCol("nhomDKId.id"); 
			       con.setValue1(new Long(id)); 
			       con.setOperator("eq"); 
			       cons.add(con); 
			   } 
			   tblDKMHDotDKHocVienChiTiets = tblDKMHDotDKHocVienChiTietService.findtblDKMHDotDKHocVienChiTietByCondition(cons, pageSize, page); 
			  
			   pageTblDKMHDotDKHocVienChiTiet = page; 
			    pageSizeTblDKMHDotDKHocVienChiTiet = pageSize; 
			 
			    pagesTblDKMHDotDKHocVienChiTiet =tblDKMHDotDKHocVienChiTietService.counttblDKMHDotDKHocVienChiTietByCondition(cons)/pageSizeTblDKMHDotDKHocVienChiTiet + 1; 
			    
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Insert tbl_DKMH_DotDK_Nhom Action");
			e.printStackTrace();
			msg.setPostMessage_b(false);
			msg.setPostMessage_s(e.getMessage());
			return Action.ERROR;
		}
	}
	private int pageSizeTblDKMHDotDKHocVienChiTiet; 
    public int getPageSizeTblDKMHDotDKHocVienChiTiet() {
        return pageSizeTblDKMHDotDKHocVienChiTiet;
 }

    public void setPageSizeTblDKMHDotDKHocVienChiTiet(int pageSizeTblDKMHDotDKHocVienChiTiet) {        this.pageSizeTblDKMHDotDKHocVienChiTiet = pageSizeTblDKMHDotDKHocVienChiTiet; }

    private int pageTblDKMHDotDKHocVienChiTiet; 
    public int getPageTblDKMHDotDKHocVienChiTiet() { return pageTblDKMHDotDKHocVienChiTiet; }

    public void setPageTblDKMHDotDKHocVienChiTiet(int pageTblDKMHDotDKHocVienChiTiet) { this.pageTblDKMHDotDKHocVienChiTiet = pageTblDKMHDotDKHocVienChiTiet; }

    private int pagesTblDKMHDotDKHocVienChiTiet; 
    public int getPagesTblDKMHDotDKHocVienChiTiet() { return pagesTblDKMHDotDKHocVienChiTiet; }

    public void setPagesTblDKMHDotDKHocVienChiTiet(int pagesTblDKMHDotDKHocVienChiTiet) { this.pagesTblDKMHDotDKHocVienChiTiet = pagesTblDKMHDotDKHocVienChiTiet; }

}
